package com.devin.service.Impl;

import com.devin.dao.MultiMapper;
import com.devin.dao.PermissionMapper;
import com.devin.dao.RoleMapper;
import com.devin.pojo.Permission;
import com.devin.pojo.Role;
import com.devin.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MultiMapper multiMapper;

    @Override
    public List<Role> getRoles(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return roleMapper.getRoles();
    }

    @Override
    public List<Role> getRoles2UserAdded() {
        return roleMapper.getRoles();
    }

    @Override
    public List<Permission> addRoleWithPermission() {
        return permissionMapper.getPermissions();
    }

    @Override
    public void addRole(Role role, Integer[] permissionIds) {
        roleMapper.addRole(role);
        multiMapper.addRoleAndPermissions(role.getId(),permissionIds);
    }

    @Override
    public Role getRoleById(Integer roleId) {
        return roleMapper.getRoleById(roleId);
    }

    @Override
    public void delPermissionByRoleId(Integer roleId, Integer permissionId) {
        roleMapper.delPermissionRoleId(roleId,permissionId);
    }

    @Override
    public List<Permission> getPermissions2Json(Integer roleId) {
        return permissionMapper.getPermissions2Json(roleId);
    }

    @Override
    public void addPermissions(Integer roleId, Integer[] permissionIds) {
        multiMapper.addPermissions(roleId,permissionIds);
    }

    @Override
    public void delRole(Integer roleId) {
        roleMapper.delRoleAndPermissions(roleId);
        roleMapper.delRole(roleId);
    }

    @Override
    public void delRolesChecked(Integer[] roleIds) {
        multiMapper.delRolesAndPermissions(roleIds);
        multiMapper.delRoles(roleIds);
    }
}
